package cn.anc.aonicardv.module.adpter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.View;

import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

import java.io.File;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import cn.anc.aonicardv.Constant;
import cn.anc.aonicardv.GlideApp;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.bean.FileBean;
import cn.anc.aonicardv.module.adpter.album.ThumbnailListAdapter;
import cn.anc.aonicardv.net.download.DownLoadManager;
import cn.anc.aonicardv.widget.SlideSelectView;

public class PreviewPhotoThumbnailListAdapter extends ThumbnailListAdapter {

    public PreviewPhotoThumbnailListAdapter(Context context, List<? extends FileBean> fileBeanList, SlideSelectView slideSelectView) {
        super(context,fileBeanList, slideSelectView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        if (slideSelectView.isUpdateItem(position)) {
            FileBean fileBean = fileBeanList.get(slideSelectView.getRightItemPosition(position));
            if (fileBean.getPath() != null) {
                GlideApp.with(context).load(Uri.fromFile( new File(  fileBean.getPath()))).placeholder(R.mipmap.image_loading).into( viewHolder.thumbnailIv);
            } else {
                String path = DownLoadManager.getInstance().getDownloadPathByRemotePath(Constant.DOWNLOAD.TYPE_PHOTO,fileBean.getPath());
                GlideApp.with(viewHolder.itemView.getContext()).load(path).placeholder(R.mipmap.image_loading).into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
                        viewHolder.thumbnailIv.setImageDrawable(resource);
                    }
                });
            }
            viewHolder.thumbnailIv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onThumbnailListItemClickListener != null)
                        onThumbnailListItemClickListener.onItemClick(slideSelectView.getRightItemPosition(position));
                }
            });
        }
        else {
            viewHolder.thumbnailIv.setOnClickListener(null);
            viewHolder.thumbnailIv.setImageBitmap(null);
        }
    }
}
